<template>
  <div class="score-case">
    <div class="table">
      <table>
        <thead>
          <tr>
            <th>编号</th>
            <th>科目</th>
            <th>成绩</th>
            <th>考试时间</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody>
          <!-- v-for遍历数组生成结构 -->
          <tr v-for="(item, index) in list" :key="item.id">
            <td>{{ index + 1 }}</td>
            <td>{{ item.subject }}</td>
            <td :class="item.score < 60 && 'red'">{{ item.score }}</td>
            <!-- 调用moment方法将时间转换成中国时间 -->
            <td>{{ format(item.date) }}</td>
            <!-- 将id传参，根据id删除本项数据 -->
            <td><a href="#" @click="del(item.id)">删除</a></td>
          </tr>
        </tbody>
        <!-- 判断，如果数组长度为0就显示 -->
        <tbody v-if="!this.list">
          <tr>
            <td colspan="5">
              <span class="none">暂无数据</span>
            </td>
          </tr>
        </tbody>
        <!-- 判断，如果数组长度为0就不显示 -->
        <tfoot v-if="this.list">
          <tr>
            <td colspan="5">
              <!-- 调用计算总分方法 -->
              <span>总分：{{ allScore }}</span>
              <!-- 调用计算平均分方法 -->
              <span style="margin-left: 50px">平均分：{{ argScore }}</span>
            </td>
          </tr>
        </tfoot>
      </table>
    </div>
    <!-- 添加按键修饰符，按下回车触发添加 -->
    <div class="form"  @keydown.enter="add">
      <div class="form-item">
        <div class="label">科目：</div>
        <div class="input">
          <!-- 双向绑定，添加trim方法，防止前后有空格 -->
          <input type="text" placeholder="请输入科目" v-model.trim="subject" />
        </div>
      </div>
      <div class="form-item">
        <div class="label">分数：</div>
        <div class="input">
          <!-- 双向绑定，添加number方法，将分数转为数字型 -->
          <input type="text" placeholder="请输入分数" v-model.number="score" />
        </div>
      </div>
      <div class="form-item">
        <div class="label"></div>
        <div class="input">
          <!-- 调用添加方法，将用户输入数据渲染到页面 -->
          <button class="submit" @click="add">添加</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
//引入moment
import moment from 'moment'
export default {
  name: 'ScoreCase',
  data () {
    return {
      // list: [
      //   {id: 15, subject: '语文', score: 89, date: new Date('2022/06/07 10:00:00')},
      //   {id: 27, subject: '数学', score: 100, date: new Date('2022/06/07 15:00:00')},
      //   {id: 32, subject: '英语', score: 56, date: new Date('2022/06/08 10:00:00')},
      //   {id: 41, subject: '物理', score: 76, date: new Date('2022/06/08 10:00:00')}
      // ],
      //从本地存储中拿数据，如果没有就为空
      list:JSON.parse(localStorage.getItem('score-case')) || [],
      subject: '',
      score: ''
    }
  },
  methods: {
    //转化中国时间的方法
    format(dat) {
      return moment(dat).format('YYYY-MM-DD HH:mm:ss')
    },
    //使用id筛选删除数据
    del(id) {
      this.list = this.list.filter(i => i.id != id )
      localStorage.setItem('score-case', JSON.stringify(this.list))
    },
    //用户输入数据添加方法
    add() {
      //判断，分数科目不能为空，分数不能小于等于0大于等于150
      if(this.score >=0 && this.score<=150 && this.subject && this.score) {
        //向数组尾部添加
        this.list.push({
          //使用at方法找到数组最后一项id值，防止重复
          id: this.list.at(-1).id + 1,
          subject: this.subject,
          score: this.score,
          date: new Date()
        })
        //添加结束清空
        this.subject = ''
        this.score = ''
        //将数组上传到本地存储中
        localStorage.setItem('score-case', JSON.stringify(this.list))
      }else{
        //提示
        alert('信息输入不正确')
      }
    },
  },
  //计算属性
  computed: {
    //计算总分方法
    allScore() {
      return this.list.reduce((p,i) => p+i.score, 0)
    },
    //计算平均分方法
    argScore() {
      return (this.allScore / this.list.length).toFixed(2)
    }
  }
};
</script>

<style lang="less">
.score-case {
  width: 1000px;
  margin: 50px auto;
  display: flex;
  .table {
    flex: 4;
    table {
      width: 100%;
      border-spacing: 0;
      border-top: 1px solid #ccc;
      border-left: 1px solid #ccc;
      th {
        background: #f5f5f5;
      }
      tr:hover td {
        background: #f5f5f5;
      }
      td,
      th {
        border-bottom: 1px solid #ccc;
        border-right: 1px solid #ccc;
        text-align: center;
        padding: 10px;
        &.red {
          color: red;
        }
      }
    }
    .none {
      height: 100px;
      line-height: 100px;
      color: #999;
    }
  }
  .form {
    flex: 1;
    padding: 20px;
    .form-item {
      display: flex;
      margin-bottom: 20px;
      align-items: center;
    }
    .form-item .label {
      width: 60px;
      text-align: right;
      font-size: 14px;
    }
    .form-item .input {
      flex: 1;
    }
    .form-item input,
    .form-item select {
      appearance: none;
      outline: none;
      border: 1px solid #ccc;
      width: 200px;
      height: 40px;
      box-sizing: border-box;
      padding: 10px;
      color: #666;
    }
    .form-item input::placeholder {
      color: #666;
    }
    .form-item .cancel,
    .form-item .submit {
      appearance: none;
      outline: none;
      border: 1px solid #ccc;
      border-radius: 4px;
      padding: 4px 10px;
      margin-right: 10px;
      font-size: 12px;
      background: #ccc;
    }
    .form-item .submit {
      border-color: #069;
      background: #069;
      color: #fff;
    }
  }
}
</style>
